package com.medical.controller;

import com.github.pagehelper.PageInfo;
import com.medical.entity.SysNhjg;
import com.medical.entity.SysUser;
import com.medical.service.NhjgService;
import com.medical.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/nhjg")
public class NhjgController extends BaseControllelr<SysNhjg>{
    @Autowired
    private NhjgService nhjgService;

    /**
     * 获取用户信息列表
     * @param model
     * @return
     */
    @RequestMapping("/getNhjgList")
    public String getNhjgList(Integer currentPage, Integer limit, HttpServletRequest request, Model model) {
        String nhjgName = request.getParameter("context");
        currentPage = currentPage != null ? currentPage : 1;
        limit = limit != null ? limit : 10;

        PageInfo<SysNhjg> pageInfo = nhjgService.getNhjgList(nhjgName, currentPage, limit);

        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("context", nhjgName);

        return "/nhjg/nhjg_list";
    }

    /**
     * 打开添加页面
     * @param model
     * @return
     */
    @RequestMapping("/openAdd")
    public String openAdd(Model model) {
        return "/nhjg/nhjg_add";
    }

    /**
     * 添加用户信息
     * @param sysNhjg
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String add(SysNhjg sysNhjg, HttpServletRequest request, Model model) {
        nhjgService.addNhjg(sysNhjg);
        return getNhjgList(1, 10, request, model);
    }

    /**
     * 打开编辑页面
     * @param model
     * @return
     */
    @RequestMapping("/openEdit")
    public String openEdit(Integer id, Model model) {
        SysNhjg sysNhjg = nhjgService.getNhjgById(id);
        model.addAttribute("nhjg",sysNhjg);
        return "/nhjg/nhjg_edit";
    }

    /**
     * 更新
     * @param sysNhjg
     * @param model
     * @return
     */
    @RequestMapping("/edit")
    public String edit(SysNhjg sysNhjg, HttpServletRequest request, Model model) {
        nhjgService.edit(sysNhjg);
        return getNhjgList(1, 10, request, model);
    }

    /**
     * 删除
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/delete")
    public String delete(Integer id, HttpServletRequest request, Model model) {
        nhjgService.delete(id);
        return getNhjgList(1, 10, request, model);
    }


}
